<template>
  <basic-dialog
    width="70%"
    title="选择供应商"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpen"
  >
    <BasicForm
      isInline
      formItemWidth="100"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :total="total"
      :tableHeight="450"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeSelect="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '../BasicList';
import basePage from '@/mixins/commodity/skusPage';
import { getTableAction } from '@/utils/getBtnsRender';
import { TABLE_COLUMNS, FORM_ITEM } from './constants';
import { getSupplierService } from '@api/sourcing/purchaseDetail';

export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
  },
  data () {
    return {
      formItemList: FORM_ITEM,
      tableInfo: {
        list: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      pagination: {
        pageNum: 1,
        pageSize: 50
      },
      formData: {
        supplierName: '',
        supplierCode: '',
        purchaseStatus: 1031,
      },
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleChoose
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表格数据
    async getTableData () {
      const res = await getSupplierService(
        {
          ...this.pagination,
          ...this.formData
        }, 
        state => this.tableInfo.loading = state
      )
      if (res?.data) {
        this.tableInfo.list = res.data?.data ?? [];
        this.total = res.data?.totalNum ?? 0;
      }      
    },
    // 选择品牌
    handleChoose (row) {
      this.$emit('getSupplierInfo', row);
      this.handleCloseDialog();
    },
    handleOpen () {
      this.formData = { 
        supplierName: '',
        supplierCode: '',
        purchaseStatus: 1031,
      }
      this.tableInfo.list = []
      this.total = 0
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
